﻿@using System.Data
@{
    ViewBag.Title = "Window";
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
    DataTable table=ViewData["msglist"] as DataTable;
    DataTable table1 = ViewData["pmenu"] as DataTable;
}
<style type="text/css">
    
     ul{float:left;}
    ul li{ float: left; margin-left: 10px; display: block;}
</style>
<input name="id" id="id" type="hidden" value="0" />
<table style="font-size: 12px; margin-left: 10px;">
    <tr>
        <td><label>菜单目录</label></td>
        <td>
            <select id="pmenu">
                <option value="0">父级菜单</option>
                @{
                    foreach (DataRow row in table1.Rows)
                    {
                        <option value="@row["Id"]">@row["MenuName"]</option>
                    }
                }
            </select>
        </td>
    </tr>
    <tr>
        <td><label>菜单名称</label></td>
        <td><input type="text" id="text" name="text" style="width: 200px;" /></td>
    </tr>
    <tr>
        <td><label>选择事件</label></td>
        <td>
            <select onchange="event()" id="sj">
                <option value="click">执行操作</option>
                <option value="view">跳转链接</option>
            </select>
        </td>
    </tr>
    <tr id="link" style="display: none;">
        <td><label>链接名称</label></td>
        <td><input type="text" id="url" name="url" style="width: 200px;" /></td>
    </tr>
    <tr id="click">
        <td><label>选择操作</label></td>
        <td>
            <div style="width: 500px;height:auto;overflow: hidden;"></div>
            <ul>
                @{
                    foreach (DataRow row in table.Rows)
                    {
                        <li id="rlist"> <input type="radio" name="msg" value="@row["Id"]"/>@row["Title"]</li>  
                    }
                }
            </ul>
        </td>
    </tr>
    <tr>
        <td><label>序号</label></td>
        <td><input type="text" id="order" name="order" style="width: 100px;" /></td>
    </tr>
</table>
<div style="width: 600px; height: 40px; margin-top: 20px; ">
    <div class="botton" style="margin-left: 50px;" onclick="edit()">保存</div>
    <div class="botton" style="margin-left: 50px;" onclick="javascript: history.go(-1);">取消</div>
    <div class="botton" style="margin-left: 50px;" onclick="publish()">发布菜单</div>
</div>
<table class="datatable">
    <tr>
     
        <th width="70">菜单名称</th>
        <th width="70">事件</th>
        <th width="70">操作</th>
        <th width="70">编辑</th>
    </tr>
    
    <tbody id="list">
        
    </tbody>


</table>
<script>

    $('input[name="msg"]:eq(0)').attr("checked", "checked");


    $("#sj").change(function() {
        var event = $("#sj").val();
        if (event == "view") {
            $("#link").show();
            $("#click").hide();
        } else {
            $("#click").show();
            $("#link").hide();
        }
    });
    getlist();

//edit menu
    function edit() {
        var text = $("#text").val();
        var url = $("#url").val();
        var msg = $('input[name="msg"]:checked').val();
        var pmenu = $("#pmenu").val();
        var order = $("#order").val();
        if (text.replace(/\s/g, "").length == 0) {
            alert("请输入菜单名称！");
            return false;
        }
        if (!$("#link").hide()) {
            if (url.replace(/\s/g, "").length == 0) {
                alert("请输入链接地址！");
                return false;
            }
        }
        var reg = /^\+?[1-9][0-9]*$/;
        if (!reg.test(order)) {
            alert("序号只能是数字，请输入数字！");
            return false;
        }
        $.post("@Url.Action("Edit")", { id: $("#id").val(), text: text, url: url, event: msg, pmenu: pmenu, order: order }, function(row) {
            if (row=="c_1") {
                location.reload();
            } else if (row == "p_3") {
                alert("父级菜单不能大于3个！");
            } else if (row == "c_5") {
                alert("子集菜单不能大于5个！");
            }
        });
    }
    function publish() {
        $.post("@Url.Action("PublishMenu")", function (data) {
           // var obj = JSON.parse(data);
          if (data == "ok") {
              alert("发布成功！");
          } else {
              alert("发布失败！");
          }
        });
    }
    ///get one menu data
    function getonedata(id, menu) {
        $.post("@Url.Action("GetOneData")", { id: id, menu: menu }, function (data) {
          
            var obj = JSON.parse(data)[0];
            if (obj.Type == "view") {
             
                $("#link").show();
                $("#click").hide();
                $("#url").val(obj.Key);
                $("#sj").val("view");
            } else {
                $("#click").show();
                $("#link").hide();
                $("#rlist input[value='"+obj.Key+"']").attr("checked","checked");
                $("#sj").val("click");
            }
            $("#text").val(obj.MenuName);
            var temp = menu == "f" ? "0" : obj.Pid;
            $("#pmenu").val(temp);
            $("#order").val(obj.Order);
            $("#id").val(obj.Id);
        });
    }

    //get menu
    function getlist() {
        $.post("@Url.Action("getlist")", function(data) {
            var obj = JSON.parse(data);
            var pmenu = obj.pmenu;
            var cmenu = obj.cmenu;
            var str = "";
            for (var i = 0; i < pmenu.length; i++) {
                str += "<tr>";
                str += "<td><label style=\"font-size:20px;\">" + pmenu[i].MenuName + "</label></td>";
                str += "<td>" + (pmenu[i].Type == "click" ? "消息" : "链接") + "</td>";
                str += "<td>" + (pmenu[i].Type == "click" ? pmenu[i].Title : pmenu[i].Key) + "</td>";
                str += "<td><a class='ope_bt' onclick=\"del('" + pmenu[i]["Id"] + "','f')\">删除</a> " +
                    "<a class='ope_bt' onclick=\"getonedata('" + pmenu[i]["Id"] + "','f')\">修改</a></td>";
                str += "</tr>";
                for (var j = 0; j < cmenu.length; j++) {
                    if (pmenu[i].Id == cmenu[j].Pid) {
                        str += "<tr>";
                        str += "<td>" + cmenu[j].MenuName + "</td>";
                        str += "<td>" + (cmenu[j].Type == "click" ? "消息" : "链接") + "</td>";
                        str += "<td>" + (cmenu[j].Type == "click" ? cmenu[j].Title : cmenu[j].Key) + "</td>";
                        str += "<td><a  class='ope_bt' onclick=\"del('" + cmenu[j]["Id"] + "','c')\">删除</a> " +
                            "<a  class='ope_bt'  onclick=\"getonedata('" + cmenu[j]["Id"] + "','c')\">修改</a></td>";
                        str += "</tr>";
                    }
                }
            }
            $("#list").html(str);
        });
    }

//delete menu
    function del(id, menu) {
        if (confirm("你确实删除该菜单吗？")) {
            $.post("@Url.Action("Del")",{id:id,menu:menu},function(row) {
                if (row == -1) {
                    alert("请删除该菜单下的子菜单！");
                    return false;
                } else if (row > 0) {
                    
                    alert("操作成功！");
                    location.reload();
                } else {
                    alert("删除失败!");
                }
            });
        }
    }

</script>